package com.ruoyi.wm.controller;

import java.util.Collection;
import java.util.List;

import com.ruoyi.wm.domain.WmApparatus;
import com.ruoyi.wm.service.IWmApparatusService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wm.domain.WmApparatusInstorageList;
import com.ruoyi.wm.service.IWmApparatusInstorageListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 耗材入库记录Controller
 * 
 * @author hpf
 * @date 2021-11-25
 */
@Controller
@RequestMapping("/wm/apparatus_inStorage")
public class WmApparatusInstorageListController extends BaseController
{
    private String prefix = "wm/apparatus_inStorage";

    @Autowired
    private IWmApparatusInstorageListService wmApparatusInstorageListService;

    @Autowired
    private IWmApparatusService wmApparatusService;

    @RequiresPermissions("wm:apparatus_inStorage:view")
    @GetMapping()
    public String apparatus_inStorage()
    {
        return prefix + "/apparatus_inStorage";
    }

    /**
     * 查询医疗器械入库记录列表
     */
    @RequiresPermissions("wm:apparatus_inStorage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmApparatusInstorageList wmApparatusInstorageList)
    {
        startPage();
        List<WmApparatusInstorageList> list = wmApparatusInstorageListService.selectWmApparatusInstorageListList(wmApparatusInstorageList);
        return getDataTable(list);
    }

    /**
     * 导出医疗器械入库记录列表
     */
    @RequiresPermissions("wm:apparatus_inStorage:export")
    @Log(title = "医疗器械入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmApparatusInstorageList wmApparatusInstorageList)
    {
        List<WmApparatusInstorageList> list = wmApparatusInstorageListService.selectWmApparatusInstorageListList(wmApparatusInstorageList);
        ExcelUtil<WmApparatusInstorageList> util = new ExcelUtil<WmApparatusInstorageList>(WmApparatusInstorageList.class);
        return util.exportExcel(list, "apparatus_inStorage");
    }

    /**
     * 新增医疗器械入库记录
     */
    @GetMapping("/add")
    public String add(WmApparatus wmApparatus, Model model)
    {
        Collection<WmApparatus> list = wmApparatusService.selectWmApparatusList(wmApparatus);
        model.addAttribute("apparatuses",list);
        return prefix + "/add";
    }

    /**
     * 新增保存医疗器械入库记录
     */
    @RequiresPermissions("wm:apparatus_inStorage:add")
    @Log(title = "医疗器械入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmApparatusInstorageList wmApparatusInstorageList)
    {
        AjaxResult result = toAjax(wmApparatusInstorageListService.insertWmApparatusInstorageList(wmApparatusInstorageList));
        int n =  wmApparatusInstorageListService.updateApparatusStock();
        return result;
    }

    /**
     * 修改医疗器械入库记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmApparatusInstorageList wmApparatusInstorageList = wmApparatusInstorageListService.selectWmApparatusInstorageListById(id);
        mmap.put("wmApparatusInstorageList", wmApparatusInstorageList);
        return prefix + "/edit";
    }

    /**
     * 修改保存医疗器械入库记录
     */
    @RequiresPermissions("wm:apparatus_inStorage:edit")
    @Log(title = "医疗器械入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmApparatusInstorageList wmApparatusInstorageList)
    {
        AjaxResult result = toAjax(wmApparatusInstorageListService.updateWmApparatusInstorageList(wmApparatusInstorageList));
        int n =  wmApparatusInstorageListService.updateApparatusStock();
        return result;
    }

    /**
     * 删除医疗器械入库记录
     */
    @RequiresPermissions("wm:apparatus_inStorage:remove")
    @Log(title = "医疗器械入库记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        AjaxResult result = toAjax(wmApparatusInstorageListService.deleteWmApparatusInstorageListByIds(ids));
        int n =  wmApparatusInstorageListService.updateApparatusStock();
        return result;
    }
}
